/*
 * Copyright (c)  2015~2020, xforceplus
 * All rights reserved.
 * Project:datacare-orm
 * Id: SimpleConnectionSource.java   2020-10-05 14-26-34
 * Author: Evan
 */
package cn.datacare.orm.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: 2015~2020
 * </p>
 * <p>
 * Company/Department: xforceplus
 * </p>
 *
 * @author Evan
 * <b>Creation Time:</b> 2020-10-05 14-26-34
 * @since V1.0
 */
class SimpleConnectionSource implements ConnectionSource {

    String driver = null;
    String password = null;
    String userName = null;
    String url = null;

    public SimpleConnectionSource(String driver, String url, String userName, String password) {
        this.driver = driver;
        this.url = url;
        this.userName = userName;
        this.password = password;
    }

    private Connection _getConn() {

        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, userName,
                    password);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("驱动未发现:" + driver, e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    @Override
    public Connection getMaster() {
        return _getConn();
    }

    @Override
    public Connection getConn(String sqlId, boolean isUpdate, String sql, List paras) {
        return _getConn();
    }


    @Override
    public boolean isTransaction() {
        return false;
    }

    @Override
    public Connection getSlave() {

        return this.getMaster();
    }

    @Override
    public void forceBegin(boolean isMaster) {

    }

    @Override
    public void forceEnd() {
    }

    @Override
    public Connection getMetaData() {
        return this.getMaster();
    }
}


